package org.wit.ecommerce.action.admin;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.wit.ecommerce.domain.Transaction;
import org.wit.ecommerce.dto.Navigator;
import org.wit.ecommerce.service.ITransactionService;


import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Namespace("/admin/transaction")
public class TransactionAction extends ActionSupport {
	
	@Autowired
	private ITransactionService transactionService;
		
	private int page = 1;

	private Navigator<Transaction> transactions;
	//view and edit
	private Long id;
	private Transaction transaction;
	private int status; 
	
	//search
	private String pin;
	private String receiverState;
	// status
	private Timestamp startDate,endDate;
	private String phone;
	
			
	
	@Action("list")
	public String list(){	
		Map<String, Object> keywords = new HashMap<String, Object>();
		keywords.put("pin", pin);
		keywords.put("receiverState", receiverState);
		// set the status make the select is unselected
		status = -1;
		keywords.put("status", status);
		keywords.put("startDate", startDate);
		keywords.put("endDate", endDate);
		keywords.put("phone", phone);
		transactions = transactionService.getTransactionsByKeywords(page, keywords);
		return SUCCESS;
	}
	
	@Action("view")
	public String view(){
		System.out.println(id);
		transaction = transactionService.getTransactionById(id);
		return SUCCESS;
	}
	
	@Action("edit")
	public String edit() {
		transaction = transactionService.getTransactionById(id);
		return SUCCESS;
	}

	@Action(value="update",results={@Result(name="success",type="redirectAction", location="list")})
	public String update() {			
		transactionService.changeTransactionStatus(id, status);
		return SUCCESS;
	}
	
	@Action(value="search",results={@Result(name="success",type="redirectAction", location="list")})
	public String search() {
		Map<String, Object> keywords = new HashMap<String, Object>();
		keywords.put("pin", pin);
		keywords.put("receiverState", receiverState);
		keywords.put("status", status);
		keywords.put("startDate", startDate);
		keywords.put("endDate", endDate);
		keywords.put("phone", phone);
		transactionService.getTransactionsByKeywords(page, keywords);
		return SUCCESS;
	}
	
	
	// Bean	
	public void setTransactionService(ITransactionService transactionService) {
		this.transactionService = transactionService;
	}
	
	public Navigator<Transaction> getTransactions() {
		return transactions;
	}
	/**
	 *  getTotalPrice
	 * @return
	 */
	public BigDecimal getTotalPrice(){
		return transaction.getAmount().multiply(transaction.getPrice());
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}	
					
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public int getStatus() {
		return status;
	}
	
	public void setStatus(int status) {
		this.status = status;
	}

	public Transaction getTransaction() {
		return transaction;
	}
	
	public void setTransaction(Transaction transaction) {
		this.transaction = transaction;
	}
	//Search
	public String getPin() {
		return pin;
	}

	public void setPin(String pin) {
		this.pin = pin;
	}

	public String getReceiverState() {
		return receiverState;
	}

	public void setReceiverState(String receiverState) {
		this.receiverState = receiverState;
	}

	public Timestamp getStartDate() {
		return startDate;
	}

	public void setStartDate(Timestamp startDate) {
		this.startDate = startDate;
	}

	public Timestamp getEndDate() {
		return endDate;
	}

	public void setEndDate(Timestamp endDate) {
		this.endDate = endDate;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	

	
	
	
	

}
